//
//  HMKeyboardView.swift
//  EmotionKeybaord
//
//  Created by ZhuDong on 16/5/30.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SnapKit

private let keyboardHeight: CGFloat = 220
private let toolbarHeight: CGFloat = 37
private let screenWidth = UIScreen.mainScreen().bounds.size.width
private let HMKeyboardViewID = "HMKeyboardViewID"

class HMKeyboardView: UIView {
    var sectionManager: HMEmotionManager = HMEmotionManager()
    override init(frame: CGRect) {
        let rect = CGRect(x: 0, y: 0, width: 0, height: keyboardHeight)
        super.init(frame: rect)
        setupUI()
        
    }
    lazy var pageContol: UIPageControl = {
        let p = UIPageControl()
        p.numberOfPages = 6
        p.currentPage = 1
        p.setValue(UIImage(named:"compose_keyboard_dot_normal"), forKey: "_pageImage")
        p.setValue(UIImage(named:"compose_keyboard_dot_selected"), forKey: "_currentPageImage")
        p.userInteractionEnabled = false
        return p
    }()
    func updatePageControl(indexPath:NSIndexPath){
        pageContol.numberOfPages = sectionManager.packages[indexPath.section].sectionEmotions.count
        pageContol.currentPage = indexPath.item
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    var toolbar = HMEmotionToolbarView()
    var collection: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        let size = CGSize(width: screenWidth, height: keyboardHeight - toolbarHeight)
        layout.itemSize = size
        layout.scrollDirection = .Horizontal
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        let colletion = UICollectionView(frame: CGRectZero, collectionViewLayout: layout)
        colletion.pagingEnabled = true
        colletion.showsVerticalScrollIndicator = false
        colletion.showsHorizontalScrollIndicator = false
        colletion.registerClass(HMCollectionViewCell.self, forCellWithReuseIdentifier: HMKeyboardViewID)
        return colletion
    }()
    func setupUI(){
        addSubview(toolbar)
        toolbar.btnClickBack = {(btn: UIButton) -> () in
            let indexPath = NSIndexPath(forRow: 0, inSection: btn.tag)
            self.collection.scrollToItemAtIndexPath(indexPath, atScrollPosition: .Left, animated: false)
        }
        toolbar.snp_makeConstraints { (make) in
            make.left.bottom.right.equalTo(self)
            make.height.equalTo(toolbarHeight)
        }
        addSubview(collection)
        collection.dataSource = self
        collection.delegate = self
        collection.snp_makeConstraints { (make) in
            make.top.left.right.equalTo(self)
            make.bottom.equalTo(toolbar.snp_top)
        }
        addSubview(pageContol)
        pageContol.snp_makeConstraints { (make) in
            make.bottom.equalTo(toolbar.snp_top).offset(-10)
            make.centerX.equalTo(self)
        }
    }
}
extension HMKeyboardView: UICollectionViewDataSource{
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return self.sectionManager.packages.count
    }
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.sectionManager.packages[section].sectionEmotions.count
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(HMKeyboardViewID, forIndexPath: indexPath) as! HMCollectionViewCell
        cell.backgroundColor = UIColor.whiteColor()
        cell.emotions = self.sectionManager.packages[indexPath.section].sectionEmotions[indexPath.item]
        return cell
    }
}
extension HMKeyboardView: UICollectionViewDelegate{
    func scrollViewDidScroll(scrollView: UIScrollView) {
        let x = collection.contentOffset.x
        var center = collection.center
        center.x = center.x + x
        let indexPaths = collection.indexPathsForVisibleItems()
        for indexPath in indexPaths{
            let cell = collection.cellForItemAtIndexPath(indexPath)
            if cell!.frame.contains(center) {
                let indexPath = collection.indexPathForCell(cell!)!
                toolbar.setToobarSelectedBtn(indexPath.section)
                updatePageControl(indexPath)
            }
        }
    }
}
